import {lazy} from './lazyLoading'

/**
 * 拼接动态路由
 */
export default (routerList, routeHideList, sysMenuList) => {
  toRoutes(routerList, routeHideList, sysMenuList)
}

function toRoutes(routerList, routeHideList, sysMenuList) {
  if (sysMenuList) {
    for (let i = 0; i < sysMenuList.length; i++) {
      let sysMenuNode = sysMenuList[i];
      if (sysMenuNode.resoure_type == 'url') {
        let code = sysMenuNode.permission;
        let routeNode = {
          name: code,
          path: '/' + code,
          component: lazy(code),
          meta: {title: sysMenuNode.menuName},
          children: []
        };
        //子节点存在
        if (sysMenuNode.sysMenuVoChild && sysMenuNode.sysMenuVoChild.length > 0) {
          toRoutes(routeNode.children, routeHideList, sysMenuNode.sysMenuVoChild);
        }
        if (code && code != '') {
          if (sysMenuNode.isShow == '1') {
            routerList.push(routeNode);
          } else {
            routeHideList.push(routeNode);
          }
        }
      } else {
        //子节点存在
        if (sysMenuNode.sysMenuVoChild && sysMenuNode.sysMenuVoChild.length > 0) {
          toRoutes(routerList, routeHideList, sysMenuNode.sysMenuVoChild);
        }
      }
    }
  }
}
